package com.cys.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cys.bean.DripData;
import com.cys.db.DBHelper;

/**
 * Servlet implementation class DripDataServlet
 */
@WebServlet("/DripDataServlet")
public class DripDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DripDataServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			Connection conn = DBHelper.getConn();
			Statement stmt = conn.createStatement();
			String sql = "select * from t_data_dev_drip_controlor order by data_time limit 1";
			ResultSet rs = stmt.executeQuery(sql);
			List<DripData> dlist = new ArrayList<DripData>();
			while (rs.next()) {
				DripData dripData = new DripData();
				dripData.setDev_drip_controlor(rs.getInt("dev_drip_controlor"));
				dripData.setData_time(rs.getString("data_time"));
				dripData.setLevel(rs.getFloat("level"));
				dripData.setSoil_wetness(rs.getFloat("soil_wetness"));
				dripData.setWater_ph(rs.getFloat("alarm_water_ph"));
				dripData.setState(rs.getInt("state"));
				dripData.setMode(rs.getInt("mode"));
				dripData.setState_waterin(rs.getInt("state_waterin"));
				dripData.setState_watermix(rs.getInt("state_watermix"));
				dripData.setAlarm_soil_wetness_high(rs.getInt("alarm_soil_wetness_high"));
				dripData.setAlarm_water_ph(rs.getInt("alarm_water_ph"));
				dripData.setAlarm_drip_startup(rs.getInt("alarm_drip_startup"));
				dripData.setAlarm_drip_stop(rs.getInt("alarm_drip_stop"));
				dlist.add(dripData);
				// System.out.println(wlist.size());
			}
			// 将图书集合放置到request之中
			request.setAttribute("dlist", dlist);
			rs.close(); // 关闭ResultSet
			stmt.close(); // 关闭Statement
			conn.close(); // 关闭Connection
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 请求转发到book_list.jsp
		request.getRequestDispatcher("LiveData.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
